package com.michaellou.Test;

import java.security.MessageDigest;
import java.util.Iterator;
import java.util.Map;
import java.util.TreeMap;
import java.util.Map.Entry;

public class DBNameHelper {
	private final static String DB_NAME="dw_api_";
	private final static String TAB_NAME="individual_index_";
	private final static int MAX_COUNT=32;
	
	public static String[] generateDB(String pt){
		//get md5 string
		if(null==pt||"".equals(pt)){
			pt+=System.nanoTime();
		}
		String con=MD5Support.MD5(pt);
		String fir5=con.substring(0,5);
		String lst5=con.substring(con.length()-5,con.length());
		
		String[] names=new String[]{DB_NAME+Integer.parseInt(fir5,16)%MAX_COUNT,TAB_NAME+Integer.parseInt(lst5,16)%MAX_COUNT};
		return names;
	}
	public static void main(String[] args) throws InterruptedException {
		int i=0;
		Long begin=System.nanoTime();
		while(i++<10000000){
//			System.out.println(i+"||"+DBNameHelper.generateDB("")[0]+"|"+DBNameHelper.generateDB("")[1]);
//			Thread.sleep(500);
			String[] names=DBNameHelper.generateDB("");
			MapTool.put(names[0]);
			MapTool.put(names[1]);
			if(i%100000==0){
				Long cost=System.nanoTime()-begin;
				System.out.println(i/100000+"||cost:"+cost+"ns");
				begin=System.nanoTime();
			}
		}
		MapTool.print();
	}
}
class MapTool{
	private static Map<String,Long> map=new TreeMap<String,Long>();
	public static void put(String k){
		if(map.containsKey(k)){
			map.put(k, map.get(k)+1);
		}else{
			map.put(k, 1L);
		}
	}
	public void get(String k){
		
	}
	public static void print(){
		Iterator<Entry<String,Long>> it =map.entrySet().iterator();
		System.out.println("---------------size:"+map.size()+"----------------");
		while(it.hasNext()){
			Entry<String,Long> en=it.next();
			System.out.println(en.getKey()+":"+en.getValue());
		}
	}
}

class MD5Support {
	private static char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
			'a', 'b', 'c', 'd', 'e', 'f' };
	public final static String MD5(String s) {

		try {
			byte[] strTemp = s.getBytes("UTF-8");
			//byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}
	public final static String MD5(String s,String encoding) {

		try {
			byte[] strTemp;
			if(encoding!=null)
				strTemp= s.getBytes(encoding);
			else
				strTemp= s.getBytes();
			//byte[] strTemp = s.getBytes();
			MessageDigest mdTemp = MessageDigest.getInstance("MD5");
			mdTemp.update(strTemp);
			byte[] md = mdTemp.digest();
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			return null;
		}
	}
	
}
